﻿using UnityEngine;
using System.Collections;
using ProgressBar;

public class CMuseum : MonoBehaviour {

	public UITexture m_Circle;
	public GameObject m_WaitingBar;
	public int 		m_nID;
	//public UILabel  m_Title;
	UIScrollView 	m_ScrollView;
	Vector3 		m_MousePos;
	public bool		m_bLoaded;

	// Use this for initialization
	void Start () {
		m_ScrollView = GetComponentInParent<UIScrollView> ();
	}
	
	// Update is called once per frame
	void Update () {
		m_Circle.transform.RotateAround (Vector3.back, Time.deltaTime * 2);

		if (m_bLoaded)
			m_WaitingBar.SetActive(false);
	}

	void OnPress(bool pressed)
	{
		if (pressed)
			m_MousePos = Input.mousePosition;

		if(!pressed) {
			if(Input.mousePosition.x - m_MousePos.x > 10)
				CStartScene.Instance.SelectItem(true);
 			else if(m_MousePos.x - Input.mousePosition.x > 10)
				CStartScene.Instance.SelectItem(false);
			else
				m_ScrollView.GetComponent<UICenterOnChild>().CenterOn(transform);
		}
	}

	void OnClick()
	{
		switch (JsonDataRead.Instance.startItems [m_nID].nType) {
		case 1: 
			JsonDataRead.Instance.FindHighVRIndex(int.Parse(JsonDataRead.Instance.startItems [m_nID].sContent));
			CSceneManager.Instance.LoadScene("StartScene", "InfoScene");
			break;
		case 2:
			JsonDataRead.Instance.FindVRIndex(int.Parse(JsonDataRead.Instance.startItems [m_nID].sContent));
			JsonDataRead.Instance.b3DVR = false;
			CSceneManager.Instance.LoadScene("StartScene", "VRScene");
			break;
		case 3:
			Application.OpenURL("http://" + JsonDataRead.Instance.startItems[m_nID].sContent);
			break;
		case 4:
			JsonDataRead.Instance.FindARIndex(int.Parse(JsonDataRead.Instance.startItems [m_nID].sContent));
			CSceneManager.Instance.LoadScene("StartScene", "ARBuyScene");
			break;		
		}
	}

	public IEnumerator SetTexture()
	{
		UITexture texture = GetComponent<UITexture> ();
		//m_Title.text = JsonDataRead.Instance.startItems [m_nID].sTitle;
		Texture tex = JsonDataRead.Instance.FindTextureByUrl (JsonDataRead.Instance.startItems [m_nID].sImageFilepath);

		if (tex == null) {
			WWW www = new WWW (JsonDataRead.Instance.DataPath + JsonDataRead.Instance.startItems [m_nID].sImageFilepath);
			yield return www;
			texture.mainTexture = www.texture;
			JsonDataRead.Instance.AddTexture(JsonDataRead.Instance.startItems [m_nID].sImageFilepath, www.texture);
			m_bLoaded = true;
		} else {
			texture.mainTexture = tex;
			m_bLoaded = true;
		}
	}
}